﻿using System.Collections.Generic;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
using Orchard.Core.Common.Utilities;
using Piedone.ContentTemplates.Services;

namespace Piedone.ContentTemplates.Models
{
    public class TemplatePart : ContentPart<TemplatePartRecord>
    {
        public bool IsTemplate
        {
            get { return Record.IsTemplate; }
            set { Record.IsTemplate = value; }
        }

        public int TemplateId
        {
            get { return Record.TemplateId; }
            set { Record.TemplateId = value; }
        }

        private readonly LazyField<ContentItem> _template = new LazyField<ContentItem>();
        public LazyField<ContentItem> TemplateField { get { return _template; } }
        public ContentItem Template
        {
            get { return _template.Value; }
        }

        private readonly LazyField<IList<ContentItem>> _availableTemplates = new LazyField<IList<ContentItem>>();
        public LazyField<IList<ContentItem>> AvailableTemplatesField { get { return _availableTemplates; } }
        public IList<ContentItem> AvailableTemplates
        {
            get { return _availableTemplates.Value; }
        }

        private readonly LazyField<IList<TemplateLoader>> _templateLoaders = new LazyField<IList<TemplateLoader>>();
        public LazyField<IList<TemplateLoader>> TemplateLoadersField { get { return _templateLoaders; } }
        public IList<TemplateLoader> TemplateLoaders
        {
            get { return _templateLoaders.Value; }
            set { _templateLoaders.Value = value; }
        }


        public class TemplateLoader
        {
            private string _name;
            public string Name
            {
                get
                {
                    if (Descriptor != null) return Descriptor.Name;
                    return _name;
                }
                set
                {
                    _name = value;
                }
            }
            public ITemplateLoaderDescriptor Descriptor { get; set; }
            public bool IsSelected { get; set; }
        }
    }

    public class TemplatePartRecord : ContentPartRecord
    {
        public virtual bool IsTemplate { get; set; }
        public virtual int TemplateId { get; set; }
    }
}